import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("data.csv",encoding="ANSI")
density = None
ratio_sugar=None
X = pd.Series([density, ratio_sugar],index=['density','ratio_sugar'])
def sigmod(inX):
    return 1.0/(1+np.exp(-inX))
def createDataSet():
    group = np.matrix([[0.697,0.46,1],
        [0.774,	0.376,1],
        [0.634,0.264,1],
        [0.608,0.318,1],
        [0.556,0.215,1],
        [0.403,0.237,1],
        [0.481,0.149,1],
        [0.437,0.211,1],
        [0.666,0.091,1],
        [0.243,0.0267,1],
        [0.245,0.057,1],
        [0.343,0.099,1],
        [0.639,0.161,1],
        [0.657,0.198,1],
        [0.36,0.37,1],
        [0.593,0.042,1],
        [0.719,0.103,1]
        ])
    labels = np.matrix([[1],[1],[1],[1],[1],[1],[1],[1],[0],[0],[0],[0],[0],[0],[0],[0],[0]])
    return group, labels
def gradAscend(dataSet, labels):
    m = dataSet.shape[0]
    n = dataSet.shape[1]
    step = 0.001
    iters = 1000
    weights = np.ones((n, 1))
    for i in range(iters):
        h = sigmod(dataSet*weights)
        errors = labels - h
        weights = weights + step * dataSet.T * errors
    return weights

dataSet, labels = createDataSet()
weights = gradAscend(dataSet, labels)
data = np.array(dataSet)
X1 = []
X2 = []
Y1 = []
Y2 = []
for i in range(8):
    X1.append(data[i, 0])
    Y1.append(data[i, 1])
for i in range(8, 16):
    X2.append(data[i, 0])
    Y2.append(data[i, 1])
plt.scatter(X1, Y1, c='red')
plt.scatter(X2, Y2, c='blue')
x1 = np.arange(0, 1, step=0.01)
print(x1)
x=np.arange(0,1,0.01)
print(weights[2, 0])
y=0.3 + (-weights[0,0]-weights[2,0]*x)/weights[1,0]
plt.plot(x,y)
plt.show()
print(gradAscend(dataSet, labels))